package org.zoomdev.zoom.dao.struct;

import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * 表结构
 */
public class TableMeta {

    private final List<ColumnMeta> columns;
    /**
     * 表名称
     */
    private String name;
    /**
     * 注释
     */
    private String comment;

    public TableMeta(String name, List<ColumnMeta> columns) {
        this.name = name;
        this.columns = columns;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    /**
     * 通过字段名称查找
     *
     * @param name
     * @return
     */
    public ColumnMeta getColumn(String name) {
        for (ColumnMeta m : columns) {
            if (StringUtils.equalsIgnoreCase(name, m.name)) {
                return m;
            }
        }
        return null;
    }


    /**
     * 获取所有字段名称
     *
     * @return
     */
    public String[] getColumnNames() {
        String[] arr = new String[columns.size()];
        for (int i = 0; i < arr.length; ++i) {
            arr[i] = columns.get(i).name;
        }
        return arr;
    }

    public List<ColumnMeta> getColumns() {
        return columns;
    }
}
